﻿using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class Clone
{
    /// <summary>
    /// 克隆一个对象
    /// </summary>
    /// <param name="obj">待克隆的对象</param>
    /// <returns>克隆的一个新的对象</returns>
    public static object Clone(object obj)
    {
        object cloned = null;
        BinaryFormatter bFormatter = new BinaryFormatter();
        using (MemoryStream memoryStream = new MemoryStream())
        {
            try
            {
                bFormatter.Serialize(memoryStream, obj);
                memoryStream.Seek(0, SeekOrigin.Begin);
                cloned = bFormatter.Deserialize(memoryStream);
            }
            catch
            {

            }
        }
        return cloned;
    }
}